Skip to content

SelectionWidget

Base classes

Name Children Inherits
GroupBox
prettyqt.widgets.groupbox
Group box frame with a title.

â‹” Inheritance diagram

graph TD
  3425563580["custom_widgets.SelectionWidget"]
  595380733["widgets.GroupBox"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2026038775["QtWidgets.QGroupBox"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  595380733 --> 3425563580
  829357528 --> 595380733
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2026038775 --> 595380733
  3620195106 --> 2026038775
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: GroupBox

Source code in prettyqt/custom_widgets/editors/selectionwidget.py
class SelectionWidget(widgets.GroupBox):
    value_changed = core.Signal(object)

    def __init__(
        self,
        label: str = "",
        layout: constants.OrientationStr = "horizontal",
        object_name: str = "selection_widget",
        **kwargs,
    ):
        super().__init__(title=label, object_name=object_name, **kwargs)
        self.set_layout(layout)
        self.widget_custom: (
            widgets.LineEdit | widgets.SpinBox | widgets.DoubleSpinBox | None
        ) = None
        self.rb_other = widgets.RadioButton()
        self.buttons: dict[widgets.RadioButton, Any] = {}

    def __iter__(self) -> Iterator[tuple[widgets.RadioButton, Any]]:
        return iter(self.buttons.items())

    def add_items(self, items: Iterable | Mapping):
        if isinstance(items, Mapping):
            for k, v in items.items():
                self.add(v, k)
        else:
            for i in items:
                if isinstance(i, tuple | list):
                    self.add(*i)
                else:
                    self.add(i)

    def select_radio_by_data(self, value):
        for rb, data in self.buttons.items():
            if data == value:
                rb.setChecked(True)
                break

    def add(self, title: str, data=None, icon: datatypes.IconType = None):
        rb = widgets.RadioButton(title)
        if icon is not None:
            rb.set_icon(icon)
        rb.toggled.connect(self.update_choice)
        self.buttons[rb] = data
        if len(self.buttons) == 1:
            with rb.signals_blocked():
                rb.set_value(True)
        self.box.add(rb)

    def add_tooltip_icon(self, text: str):
        label = widgets.Label(text)
        label.setToolTip(text)
        icon = iconprovider.get_icon("mdi.help-circle-outline")
        pixmap = icon.pixmap(20, 20)
        label.setPixmap(pixmap)
        self.box.add(label)

    def add_custom(
        self,
        label: str = "Other",
        typ: TypeStr = "string",
        default: float | str | None = None,
        regex: str | None = None,
    ):
        widget: widgets.LineEdit | widgets.SpinBox | widgets.DoubleSpinBox
        match typ:
            case "string":
                widget = widgets.LineEdit()
                if default is not None:
                    if not isinstance(default, str):
                        raise TypeError(default)
                    widget.set_value(default)
                if regex:
                    widget.set_regex_validator(regex)
            case "int":
                widget = widgets.SpinBox()
                if default is not None:
                    if not isinstance(default, int):
                        raise TypeError(default)
                    widget.set_value(default)
            case "float":
                widget = widgets.DoubleSpinBox()
                if default is not None:
                    if not isinstance(default, int | float):
                        raise TypeError(default)
                    widget.set_value(default)
            case _:
                raise ValueError(typ)
        self.widget_custom = widget
        # TODO: Enable this or add BAR radio and option.
        widget.set_disabled()
        self.rb_other.setText(label)
        self.rb_other.toggled.connect(widget.set_enabled)
        self.rb_other.toggled.connect(self.update_choice)
        widget.value_changed.connect(lambda: self.update_choice(True))
        layout = widgets.HBoxLayout()
        layout.add(self.rb_other)
        layout.add(widget)
        self.box.add(layout)

    def current_choice(self) -> Any:
        for k, v in self.buttons.items():
            if k.isChecked():
                return v
        if self.rb_other.isChecked() and self.widget_custom is not None:
            return self.widget_custom.get_value()
        return None

    @core.Slot(bool)
    def update_choice(self, checked: bool):
        if not checked:
            return
        if (choice := self.current_choice()) is not None:
            self.value_changed.emit(choice)

    def set_value(self, value):
        self.select_radio_by_data(value)

    def get_value(self):
        return self.current_choice()

:fa-brands-github: Show source on GitHub